﻿using Microsoft.Extensions.Configuration;

namespace StudyCompanion.Commons.Config;

public class AppSettings
{
    /// <summary>
    ///     构造函数
    /// </summary>
    /// <param name="configuration"></param>
    public AppSettings(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    /// <summary>
    /// </summary>
    public static IConfiguration Configuration { get; set; }

    /// <summary>
    ///     根据路径返回配置
    /// </summary>
    /// <param name="sectionsPath"></param>
    /// <returns></returns>
    public static string GetValue(string sectionsPath)
    {
        try
        {
            return Configuration[sectionsPath];
        }
        catch (Exception)
        {
        }

        return "";
    }

    /// <summary>
    ///     根据路径返回配置
    /// </summary>
    /// <param name="sectionsPath"></param>
    /// <returns></returns>
    public static T GetValue<T>(string sectionsPath)
    {
        return Configuration.GetValue<T>(sectionsPath);
    }

    /// <summary>
    ///     根据路径返回配置
    /// </summary>
    /// <param name="sectionsPath"></param>
    /// <returns></returns>
    public static string GetValue(string[] sectionsPath)
    {
        try
        {
            if (sectionsPath.Any()) return Configuration[string.Join(":", sectionsPath)];
        }
        catch (Exception)
        {
        }

        return "";
    }

    /// <summary>
    ///     根据路径返回配置
    /// </summary>
    /// <param name="sectionsPath"></param>
    /// <returns></returns>
    public static T GetValue<T>(string[] sectionsPath)
    {
        return Configuration.GetValue<T>(string.Join(":", sectionsPath));
    }
}